﻿using Apps.IService;
using Apps.Repository;
using Apps.Service;
using Autofac;
using Apps.Repository.UnitOfWork;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Apps.Extensions.Logger;
using Autofac.Extras.DynamicProxy;
using Castle.DynamicProxy;

namespace Apps.Extensions
{
    public class AutofacModuleRegister : Module
    {
        protected override void Load(ContainerBuilder builder)
        {
            // 注册数据上下文
            builder.RegisterType<SqlDbContext>().AsSelf().InstancePerLifetimeScope();
            // 注册拦截器
            builder.RegisterType<LoggingInterceptor>();

            builder.RegisterGeneric(typeof(BaseRepository<,>))
                   .As(typeof(IBaseRepository<,>))
                   .UsingConstructor(typeof(SqlDbContext))
                   .InstancePerLifetimeScope();

            // 注册 UnitOfWork 和相关仓储
            builder.RegisterType<UnitOfWork>().As<IUnitOfWork>().InstancePerLifetimeScope();
            builder.RegisterType<SysUserService>().As<ISysUserService>().InstancePerLifetimeScope().EnableInterfaceInterceptors();

            // 注册服务并启用拦截器
            builder.RegisterGeneric(typeof(BaseService<,>))
                   .As(typeof(IBaseService<,>))
                   .InstancePerLifetimeScope()
                   .EnableInterfaceInterceptors()
                   .InterceptedBy(typeof(LoggingInterceptor));


            // 注册其他服务 (如果需要)


           
            // builder.RegisterType<SomeOtherService>().As<ISomeOtherService>().InstancePerLifetimeScope();
        }
    }
}
